package com.zhangyue.wqercs.guideactivity;

import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.ImageView;

class MyPageTransformer implements ViewPager.PageTransformer,ViewPager.OnPageChangeListener {
    private boolean pageChanged = false;
    private int indexPage = 0;
    private static int degree = 15;

    @Override
    public void transformPage(@NonNull View view, float position) {
        ViewGroup viewGroup = view.findViewById(R.id.pv);
        ImageView bg1 = view.findViewById(R.id.bg1);
        ImageView bg2 = view.findViewById(R.id.bg2);
        final MyScrollView myScrollView = viewGroup.findViewById(R.id.myScrollView);
        ArgbEvaluator argbEvaluator = new ArgbEvaluator();
        View parent = (View)view.getParent();
        int bg1Color = view.getContext().getResources().getColor(R.color.bg1_green);
        int bg2Color = view.getContext().getResources().getColor(R.color.bg2_blue);
        int bg3Color = view.getContext().getResources().getColor(R.color.bg3_green);
        int bgColor = bg1Color;
        if(indexPage == (int)view.getTag()){
            switch (indexPage){
                case 0:
                        bgColor = (int)argbEvaluator.evaluate(Math.abs(position),bg1Color,bg2Color);
                    break;
                case 1:
                    if(position>=0){
                        bgColor = (int)argbEvaluator.evaluate(Math.abs(position),bg2Color,bg3Color);
                    }else{
                        bgColor = (int)argbEvaluator.evaluate(Math.abs(position),bg2Color,bg1Color);
                    }
                    break;
                case 2:
                    bgColor = (int)argbEvaluator.evaluate(Math.abs(position),bg3Color,bg2Color);
                    break;
            }
            parent.setBackgroundColor(bgColor);
        }
        if(position == 0){
            if(pageChanged){
                bg1.setVisibility(View.VISIBLE);
                ObjectAnimator anim1 = ObjectAnimator.ofFloat(bg1, "translationX", 0, -bg1.getWidth());
                anim1.setDuration(400);
                anim1.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                    @Override
                    public void onAnimationUpdate(ValueAnimator animation) {
                        myScrollView.smoothScrollTo((int)(myScrollView.getWidth()*animation.getAnimatedFraction()),0);
                    }
                });
                anim1.start();

                ObjectAnimator anim2 = ObjectAnimator.ofFloat(bg1, "translationX", bg2.getWidth(), 0);
                anim2.setDuration(400);
                anim2.start();
                pageChanged = false;
            }
        }else if(position == -1 || position == 1){
                bg1.setVisibility(View.GONE);
                bg1.setTranslationX(0);
                bg2.setTranslationX(0);
                myScrollView.smoothScrollTo(0,0);
        }else {
            int width = bg1.getWidth();
            int height = bg1.getHeight();
            viewGroup.setPivotX(width/2);
            viewGroup.setPivotY(height);
            viewGroup.setRotation(degree*position);
        }
    }


    @Override
    public void onPageScrolled(int i, float v, int i1) {

    }

    @Override
    public void onPageSelected(int i) {
        pageChanged = true;
        indexPage = i;
    }

    @Override
    public void onPageScrollStateChanged(int i) {

    }
}
